'use strict';

const conn = require('../../libs/Conn');
const util = require('../../libs/Utils');
const moment = require('moment');

module.exports = async function (params) {

    if(params.eng_name){
        if(params.eng_name === ''){
            return {success: false,message:'请输入工程名称'}
        }else{
            let engineeringNameSql = `SELECT * FROM engineering_file WHERE engineering_name = '${params.eng_name}' AND delete_sign = 1`;
            let rec = await conn(engineeringNameSql);
            if(rec.length>0){
                return {success: false,message:'此工程名称已存在'}
            }
        }
    }else{
        return {success: false,message:'请输入工程名称'}
    }

    let  dataBaseSql = `SELECT engineering_no FROM engineering_file ORDER BY engineering_no DESC LIMIT 1;SELECT * FROM engineering_file WHERE engineering_id = '${params.engineering_id}' AND delete_sign = 1 ; SELECT * FROM engineering_information_file WHERE  engineering_id = '${params.engineering_id}' AND delete_sign = 1`;
    let dataBaseData = await conn(dataBaseSql);

    let goalEngineering = {};
    let goalEngineering_information = {};

    if(dataBaseData[1].length>0){
        goalEngineering = dataBaseData[1][0]
    }else{
        return {success: false,message:'没有找到可以克隆的工程'}
    }
    if(dataBaseData[1].length>0){
        goalEngineering_information = dataBaseData[2][0]
    }else{
        return {success: false,message:'没有找到可以克隆的工程'}
    }

    let engineering_id = util.uuid();
    let engineering_no = '';
    let nowYear = moment().format('YYYY');
    let year;
    if(dataBaseData[0].length>0){
        year = dataBaseData[0][0].engineering_no.substring(0,4);
        if(year===nowYear){
            engineering_no =  parseInt(dataBaseData[0][0].engineering_no)+1
        }else{
            engineering_no =  nowYear+'00001'
        }
    }else{
        engineering_no =  nowYear+'00001'
    }
    let regionSql = ``;

    let progressSql = `INSERT INTO engineering_progress_file (engineering_id,progress_id) VALUES ('${engineering_id}','${util.uuid()}');INSERT INTO engineering_progress_completion (engineering_id,information_id) VALUES ('${engineering_id}','${util.uuid()}');INSERT INTO engineering_progress_filing (engineering_id) VALUES ('${engineering_id}');`;
    let progressOtherSql = `INSERT INTO engineering_progress_other_data (engineering_id) VALUES ('${engineering_id}');`;
    goalEngineering.engineering_id = engineering_id;
    goalEngineering.engineering_no = engineering_no;
    goalEngineering.engineering_name = params.eng_name;
    delete goalEngineering.create_time;
    delete goalEngineering.update_time;


    goalEngineering_information.engineering_id = engineering_id;
    if(params.aboveGround_floorA && params.aboveGround_floorA !=='') goalEngineering_information.aboveGround_floor = params.aboveGround_floorA;
    if(params.underGround_floorA && params.underGround_floorA !=='') goalEngineering_information.underGround_floor = params.underGround_floorA;
    if(params.engineering_area && params.engineering_area !=='') goalEngineering_information.engineering_area = params.engineering_area;
    delete goalEngineering_information.create_time;
    delete goalEngineering_information.update_time;

    let qualityObj = {
        engineering_id:engineering_id
    };

    let certificateSql = ``;
    for(let i = 1; i<7;i++){
        certificateSql += `INSERT INTO certificate_file (engineering_id,certificate_type,unit_type) VALUES ('${engineering_id}', '${1}','${i}');`
    }
    for(let i = 1; i<7;i++){
        certificateSql += `INSERT INTO certificate_file (engineering_id,certificate_type,unit_type) VALUES ('${engineering_id}', '${2}','${i}');`
    }
    let sql = 'INSERT INTO engineering_file SET ?;INSERT INTO engineering_information_file SET ?;INSERT INTO quality_file SET ? ;'+certificateSql + regionSql+progressSql+progressOtherSql;
    await conn(sql, [goalEngineering,goalEngineering_information,qualityObj]);
    return {success: true};
};